import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import store from "../store"
import { ref } from "vue";
const routes: Array<RouteRecordRaw> = [
    {
        path: "",
        redirect: "/login"
    }, {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/Login.vue')
    }, {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/home/Home.vue'),
        //二级路由
        children: [

             {
                path: 'nav',
                name: "Nav",
                component: () => import('@/components/navigation/Navigation.vue'),
                meta:{
                    roles: ['admin', 'movie']
                },
            },
            {
                path: 'ac',
                name: "AcInfo",
                component: () => import('@/components/accountpage/accountpage.vue'),
                meta: {
                    roles: ['admin']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
            {
                path: 'arrange',
                name: "Arrange",
                component: () => import('@/components/arrange/Arrange.vue'),
                meta: {
                    roles: ['movie']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
            {
                path: 'movie',
                name: "Movie",
                component: () => import('@/components/moviemanagement/Movie.vue'),
                meta: {
                    roles: ['movie']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
            {
                path: 'cinema',
                name: "Cinema",
                component: () => import('@/components/cinemamanagement/Cinema.vue'),
                meta: {
                    roles: ['movie']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
            {
                path: 'order',
                name: "Order",
                component: () => import('@/components/ordermanagement/Order.vue'),
                meta: {
                    roles: ['movie']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
            {
                path: 'seat',
                name: "Seat",
                component: () => import('@/components/seatmanagement/Seat.vue'),
                meta: {
                    roles: ['movie']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
            {
                path: 'room',
                name: "Room",
                component: () => import('@/components/roommanagement/Room.vue'),
                meta: {
                    roles: ['movie']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
            {
                path: 'discuss',
                name: "Discuss",
                component: () => import('@/components/discussmanagement/Discuss.vue'),
                meta: {
                    roles: ['admin']
                },
                beforeEnter: (to, from, next) => {
                    let role:any=ref(store.state);
                    if((to.meta.roles as any).includes(role.value.user.roleTag))next();
                    else{router.push('/error404')}
                }
            },
        ]
    },
    {
        path:'/error404',
        name: 'ERROR404',
        component:()=>import('@/views/error/error404.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});
// 全局守卫：登录拦截 本地没有存token,请重新登录
router.beforeEach((to, from, next) => {
	// 判断有没有登录
	if (!sessionStorage.getItem('token')){
		if (to.name == "Login") {
			next();
		} 
        else {
			router.push('/login')
		}
	} 
    else {
		next();
	}
});
export default router;